package main

import (
	"database/sql"
	"errors"
	"fmt"
	pb "ihos/ihos"
	"log"
)

type basicinfo struct {
	id                                     int32
	xb                                     int32
	xm, sfz, sjhm                          string
	wx_gender                              int32
	wx_nickname, wx_avatarurl, wx_province string
	wx_city, wx_country, wx_account        string
	wx_unionid, wx_openid, wx_appid        string
	add_dz, add_yb                         string
	add_jd, add_wd                         float32
	add_sheng, add_shi, add_qu             string
}

func (w *basicinfo) GetFromDB(id int32) error {
	sql := "SELECT id,xb,xm,sfz,sjhm,wx_gender,wx_nickname,wx_avatarurl,wx_province,wx_city,wx_country,wx_account,wx_unionid,wx_openid,wx_appid,add_dz,add_yb,add_jd,add_wd,add_sheng,add_shi,add_qu FROM basicinfo_private WHERE id=$1"
	return DBConn.db.QueryRow(sql, id).Scan(&w.id, &w.xb, &w.xm, &w.sfz, &w.sjhm, &w.wx_gender, &w.wx_nickname, &w.wx_avatarurl, &w.wx_province, &w.wx_city, &w.wx_country, &w.wx_account, &w.wx_unionid, &w.wx_openid, &w.wx_appid, &w.add_dz, &w.add_yb, &w.add_jd, &w.add_wd, &w.add_sheng, &w.add_shi, &w.add_qu)
}

func (w *basicinfo) GetBasicInfoList() (bis []*basicinfo, err error) {
	log.Println("GetBasicInfoList", w)
	out := make([]*basicinfo, 0)
	var rows *sql.Rows
	sqlStr := ""
	if w.id > 0 {
		sqlStr = "SELECT id,xb,xm,sfz,sjhm,wx_gender,wx_nickname,wx_avatarurl,wx_province,wx_city,wx_country,wx_account,wx_unionid,wx_openid,wx_appid,add_dz,add_yb,add_jd,add_wd,add_sheng,add_shi,add_qu FROM basicinfo_private WHERE id=" + fmt.Sprint(w.id)
	} else {
		sqlStr = "SELECT id,xb,xm,sfz,sjhm,wx_gender,wx_nickname,wx_avatarurl,wx_province,wx_city,wx_country,wx_account,wx_unionid,wx_openid,wx_appid,add_dz,add_yb,add_jd,add_wd,add_sheng,add_shi,add_qu FROM basicinfo_private"
		cond := make([]string, 0)
		if len(w.sfz) > 0 {
			cond = append(cond, "sfz like '%"+w.sfz+"%'")
		}
		if len(w.xm) > 0 {
			cond = append(cond, "xm like '%"+w.xm+"%'")
		}
		if len(w.sjhm) > 0 {
			cond = append(cond, "sjhm like '%"+w.sjhm+"%'")
		}
		if len(cond) > 0 {
			sqlStr += " WHERE "
			sep := ""
			for _, v := range cond {
				sqlStr += sep + v
				sep = " AND "
			}
		}
		sqlStr += " ORDER BY id DESC LIMIT 100"
	}
	log.Println("SQL:", sqlStr)
	rows, err = DBConn.db.Query(sqlStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	defer rows.Close()
	for rows.Next() {
		w := &basicinfo{}
		if err = rows.Scan(&w.id, &w.xb, &w.xm, &w.sfz, &w.sjhm, &w.wx_gender, &w.wx_nickname, &w.wx_avatarurl, &w.wx_province, &w.wx_city, &w.wx_country, &w.wx_account, &w.wx_unionid, &w.wx_openid, &w.wx_appid, &w.add_dz, &w.add_yb, &w.add_jd, &w.add_wd, &w.add_sheng, &w.add_shi, &w.add_qu); err != nil {
			log.Println(err)
			//return nil, err
		} else {
			// log.Println(w)
			out = append(out, w)
		}
	}
	log.Println("OUT:", out)
	if len(out) > 0 {
		return out, nil
	} else {
		return nil, errors.New("Not found")
	}

}

func (w *basicinfo) SaveToDB(id int32) error {
	sql := ""
	var err error
	if id > 0 { //update
		sql = "UPDATE basicinfo_private set xb=$1,xm=$2,sfz=$3,sjhm=$4,wx_gender=$5,wx_nickname=$6,wx_avatarurl=$7,wx_province=$8,wx_city=$9,wx_country=$10,wx_account=$11,wx_unionid=$12,wx_openid=$13,wx_appid=$14,add_dz=$15,add_yb=$16,add_jd=$17,add_wd=$18,add_sheng=$19,add_shi=$20,add_qu=$21 WHERE id=$22 RETURNING id"
		err = DBConn.db.QueryRow(sql, w.xb, w.xm, w.sfz, w.sjhm, w.wx_gender, w.wx_nickname, w.wx_avatarurl, w.wx_province, w.wx_city, w.wx_country, w.wx_account, w.wx_unionid, w.wx_openid, w.wx_appid, w.add_dz, w.add_yb, w.add_jd, w.add_wd, w.add_sheng, w.add_shi, w.add_qu, w.id).Scan(&w.id)
	} else { //insert
		sql = "INSERT INTO basicinfo_private(xb,xm,sfz,sjhm,wx_gender,wx_nickname,wx_avatarurl,wx_province,wx_city,wx_country,wx_account,wx_unionid,wx_openid,wx_appid,add_dz,add_yb,add_jd,add_wd,add_sheng,add_shi,add_qu) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21) RETURNING id"
		err = DBConn.db.QueryRow(sql, w.xb, w.xm, w.sfz, w.sjhm, w.wx_gender, w.wx_nickname, w.wx_avatarurl, w.wx_province, w.wx_city, w.wx_country, w.wx_account, w.wx_unionid, w.wx_openid, w.wx_appid, w.add_dz, w.add_yb, w.add_jd, w.add_wd, w.add_sheng, w.add_shi, w.add_qu).Scan(&w.id)
	}
	return err
}

func (w *basicinfo) GetByOpenID(oid string) error {
	sql := "SELECT id,xb,xm,sfz,sjhm,wx_gender,wx_nickname,wx_avatarurl,wx_province,wx_city,wx_country,wx_account,wx_unionid,wx_openid,wx_appid,add_dz,add_yb,add_jd,add_wd,add_sheng,add_shi,add_qu FROM basicinfo_private WHERE wx_openid=$1"
	return DBConn.db.QueryRow(sql, oid).Scan(&w.id, &w.xb, &w.xm, &w.sfz, &w.sjhm, &w.wx_gender, &w.wx_nickname, &w.wx_avatarurl, &w.wx_province, &w.wx_city, &w.wx_country, &w.wx_account, &w.wx_unionid, &w.wx_openid, &w.wx_appid, &w.add_dz, &w.add_yb, &w.add_jd, &w.add_wd, &w.add_sheng, &w.add_shi, &w.add_qu)
}

func (w *basicinfo) GetBySFZ(sfz string) error {
	sql := "SELECT id,xb,xm,sfz,sjhm,wx_gender,wx_nickname,wx_avatarurl,wx_province,wx_city,wx_country,wx_account,wx_unionid,wx_openid,wx_appid,add_dz,add_yb,add_jd,add_wd,add_sheng,add_shi,add_qu FROM basicinfo_private WHERE sfz=$1"
	return DBConn.db.QueryRow(sql, sfz).Scan(&w.id, &w.xb, &w.xm, &w.sfz, &w.sjhm, &w.wx_gender, &w.wx_nickname, &w.wx_avatarurl, &w.wx_province, &w.wx_city, &w.wx_country, &w.wx_account, &w.wx_unionid, &w.wx_openid, &w.wx_appid, &w.add_dz, &w.add_yb, &w.add_jd, &w.add_wd, &w.add_sheng, &w.add_shi, &w.add_qu)
}

func (w *basicinfo) fromPB(in *pb.BasicInfoL1) *basicinfo {
	// log.Println("BasicInfo fromPB", in)
	//基本信息OpenID唯一，存在则读取记录
	if in.GetWX() != nil && len(in.GetWX().GetOpenId()) > 0 {
		if err := w.GetByOpenID(in.GetWX().GetOpenId()); err != nil {
			w.id = 0
		}
	}
	// w.id = in.GetGRXX().GetID()
	w.xb = int32(in.GetXB())
	w.xm = in.GetXM()
	w.sfz = in.GetSFZ()
	w.sjhm = in.GetSJHM()
	w.wx_gender = int32(in.GetWX().GetGender())
	w.wx_nickname = in.GetWX().GetNickName()
	w.wx_avatarurl = in.GetWX().GetAvatarUrl()
	w.wx_province = in.GetWX().GetProvince()
	w.wx_city = in.GetWX().GetCity()
	w.wx_country = in.GetWX().GetCountry()
	w.wx_account = in.GetWX().GetAccount()
	w.wx_unionid = in.GetWX().GetUnionId()
	w.wx_openid = in.GetWX().GetOpenId()
	w.wx_appid = in.GetWX().GetAppId()
	return w
}

func (w *basicinfo) fromHZPB(in *pb.HuanZheInfoL3) *basicinfo {
	w.fromPB(in.GetGRXX())
	w.add_dz = in.GetJTDZ().GetDZ()
	w.add_yb = in.GetJTDZ().GetYB()
	w.add_jd = in.GetJTDZ().GetJD()
	w.add_wd = in.GetJTDZ().GetWD()
	w.add_sheng = in.GetJTDZ().GetSHENG()
	w.add_shi = in.GetJTDZ().GetSHI()
	w.add_qu = in.GetJTDZ().GetQU()
	return w
}

func (w *basicinfo) toPB() *pb.BasicInfoL1 {
	d := &pb.BasicInfoL1{
		ID:  w.id,
		XM:  w.xm,
		XB:  pb.GENDER(w.xb),
		SFZ: w.sfz,
		// CSRQ: 0,
		SJHM: w.sjhm,
		WX: &pb.WeiXinInfo{
			NickName:  w.wx_nickname,
			AvatarUrl: w.wx_avatarurl,
			Gender:    pb.GENDER(w.wx_gender),
			Province:  w.wx_province,
			City:      w.wx_city,
			Country:   w.wx_country,
			Account:   w.wx_account,
			UnionId:   w.wx_unionid,
			OpenId:    w.wx_openid,
			AppId:     w.wx_appid,
		},
	}
	return d
}
